package com.yanlin.servicedriveruser.service.impl;

import com.yanlin.internalcommon.dto.ResponseResult;
import com.yanlin.internalcommon.dto.DriverUser;
import com.yanlin.servicedriveruser.mapper.UserMapper;
import com.yanlin.servicedriveruser.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yanlin.internalcommon.constant.CommonStatusEnum.DRIVER_NOT_EXISTS;
import static com.yanlin.internalcommon.constant.DriverCarConstant.DRIVER_STATE_VALID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-06-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, DriverUser> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult getUser(String driverPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone", driverPhone);
        List<DriverUser> driverUsers = userMapper.selectByMap(map);
        System.out.println("司机：+" + driverUsers);
        if (driverUsers.size() < 1) {
            return ResponseResult.fail(driverUsers.get(0));
        }
        return ResponseResult.success(driverUsers.get(0));
    }

    @Override
    public ResponseResult addUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setCreateTime(now);
        driverUser.setUpdateTime(now);
        userMapper.insert(driverUser);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateUser(DriverUser driverUser) {
        driverUser.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<DriverUser> getUserByDriverPhone(String driverPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone", driverPhone);
        map.put("state", DRIVER_STATE_VALID);
        List<DriverUser> driverUsers = userMapper.selectByMap(map);
        System.out.println("司机：+" + driverUsers);
        if (driverUsers.isEmpty()) {
            return ResponseResult.fail(DRIVER_NOT_EXISTS.getCode(), DRIVER_NOT_EXISTS.getValue());
        }
        return ResponseResult.success(driverUsers.get(0));
    }
}
